/**
 * 
 */
package truerefactor.graph;

import java.util.List;

/**
 * CodeNode - Abstract base class for all nodes to be stored inside of a
 * CodeGraph.
 * 
 * @author Isaac Griffith
 */
public abstract class CodeNode {

    /**
     * the unique id
     */
    protected long uid = 0;
    /**
     * code
     */
    protected String code = "";
    /**
     * the identifier associated with this piece of code (not necessarily
     * unique)
     */
    protected String identifier = "";

    /**
     * Creates a new instance of CodeNode with the given identifier and code
     * 
     * @param identifier
     *            identifier of this nod
     * @param code
     *            code for this node
     */
    public CodeNode(long uid, String identifier, String code)
    {
        this.identifier = identifier;
        this.code = code;
        this.uid = uid;
    }

    /**
     * Adds a child node directly connected to this node, with an edge from this
     * node to the child Where parent and child form a hierarchical
     * relationship, that is not neccesarily modeled in the graph.
     * 
     * @param child
     *            node connected to this node
     */
    public abstract void add(CodeNode child);

    /**
     * Removes the provided child from the list of children
     * 
     * @param child
     *            child node to be removed
     */
    public abstract void remove(CodeNode child);

    /**
     * @param identfier
     *            identifier of the child to be found
     * @return the child associated with this node with the provided identifier.
     */
    public abstract CodeNode getChild(String identfier);

    /**
     * @return list of all children associated with this node
     */
    public abstract List<CodeNode> getChildren();

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + (int) (uid ^ (uid >>> 32));
        return result;
    }

    /**
     * @return this node's identifier
     */
    public String getIdentifier()
    {
        if (identifier == null)
            identifier = "";
        return identifier;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            return true;
        }
        if (obj == null)
        {
            return false;
        }
        if (!(obj instanceof CodeNode))
        {
            return false;
        }
        CodeNode other = (CodeNode) obj;
        if (uid != other.uid)
        {
            return false;
        }
        return true;
    }

    /**
     * @return
     */
    public String getCode()
    {
        return code;
    }

    /**
     * @return
     */
    public long getUID()
    {
        return uid;
    }
}
